package com.gxa.pingan.controller;

import com.alibaba.excel.EasyExcel;
import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.OrderVo;
import com.gxa.pingan.pojo.User;
import com.gxa.pingan.service.UserService;
import com.gxa.pingan.utils.CastListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenJin
 * @date 2021/10/25 15:32
 * Description:
 **/
@CrossOrigin
@RestController
@RequestMapping(value = "/user")
@Api(value = "执行用户操作的控制器", tags = "用户接口的api")
public class UserController {

    @Resource
    private UserService userService = null;

    /**
     * 导出用户信息的接口方法
     * 用户可通过列表第一列的选择按钮选择需要导出的内容
     *
     * @return Map--需要给成功的提示吗?
     * @throws IOException 异常
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出用户信息的接口方法")
    public ResponseEntity<byte[]> exportOrderList() throws IOException, BusinessException {
        // 查询到需要导出的集合
        List<User> lists = userService.upload();
        System.out.println(lists);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 语雀的核心写操作
        EasyExcel.write(outputStream, User.class).sheet("用户信息表").doWrite(lists);
        // 缓冲数组
        byte[] bytes = outputStream.toByteArray();
        // 获取请求头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求头,告诉浏览器，以附件方式打开，而不是在页面上浏览
        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode("用户信息表.xlsx", "utf-8"));
        //httpHeaders.setContentDispositionFormData("attachment", "车主卡信息表.xlsx");
        return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
    }

    /**
     * 添加用户
     *
     * @param id 用户编号
     * @return
     * @throws BusinessException
     */
    @PostMapping("/getUser")
    @ApiOperation("修改时获取用户信息")
    @ApiImplicitParam(value = "id", paramType = "query")
    public Map<String, Object> getById(Long id) throws BusinessException {
        Map<String, Object> userById = userService.getUserById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("获取成功");
        jsonResult.setData(userById);
        return jsonResult.getDataMap();
    }

    /**
     * 添加用户
     *
     * @param user 用户对象
     * @return
     * @throws BusinessException
     */
    @PostMapping("/insert")
    @ApiOperation("添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "user", paramType = "body")
    })
    public Map<String, Object> insert(@RequestBody Map<String, Object> user) throws BusinessException {
        userService.addUser(user);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }

    /**
     * 修改用户
     *
     * @param user 用户对象
     * @return
     * @throws BusinessException
     */
    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    @ApiImplicitParam(name = "user", value = "user", paramType = "body", required = true)
    public Map<String, Object> update(@RequestBody Map<String, Object> user) throws BusinessException {
        userService.update(user);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    /**
     * 分页搜索用户列表
     *
     * @param value 搜索框的值
     * @param page  页码
     * @param size  每页长度
     * @return
     * @throws BusinessException
     */
    @PostMapping("/get")
    @ApiOperation("根据用户姓名或手机号查找用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "value", value = "value", paramType = "query", required = false),
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = true)
    })
    public Map<String, Object> get(String value, Integer page, Integer size) throws BusinessException {
        Integer count = userService.search(value);
        List<User> search = userService.getSearch(value, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("搜索成功");
        jsonResult.setData(search);
        jsonResult.put("count", count);
        return jsonResult.getDataMap();
    }

    /**
     * 分页获取全部用户信息列表
     *
     * @param page 页码
     * @param size 每页长度
     * @return 返回分页用户列表
     * @throws BusinessException
     */
    @PostMapping("/getList")
    @ApiOperation("分页获取全部用户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = true)
    })
    public Map<String, Object> getList(Integer page, Integer size) throws BusinessException {
        Integer list = userService.getList();
        List<User> listByPage = userService.getListByPage(page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("获取成功");
        jsonResult.setData(listByPage);
        jsonResult.put("count", list);
        return jsonResult.getDataMap();
    }


}
